package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.constant.Constant;
import com.yanzhu.service.CommentService;
import com.yanzhu.service.UserService;
import com.yanzhu.service.VideoService;
import com.yanzhu.service.model.Comment;
import com.yanzhu.service.model.Message;
import com.yanzhu.service.model.User;
import com.yanzhu.service.model.Video;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: 方炎柱
 * @Description: 点赞处理
 * @Date: Created in 22:02 2018/1/21
 * @Modified:
 */
@AllArgsConstructor
@RestController
public class CommentAPI {
    private CommentService commentService;

    private VideoService videoService;

    private UserService userService;

    @RequestMapping(value="/commentVideo")
    public ResponseBean commentVideo(HttpServletRequest request) throws Exception {
        String uid = request.getParameter("uid");
        String vid = request.getParameter("vid");

        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少视频ID参数!");

        Video video = videoService.selectById(Long.parseLong(vid));
        if(video == null)
            return ResponseBean.fail("该视频不存在!");

        User user = userService.queryUserInfo(Long.parseLong(uid));
        if(user == null)
            return ResponseBean.fail("该改用户不存在!");

        Comment comment = new Comment();
        comment.setUid(Long.parseLong(uid));
        comment.setVid(Long.parseLong(vid));
        String content = request.getParameter("content");
        if(!(content == null || content.equals("")))
            comment.setContent(content);
        comment.setComment_time((int)(new Date().getTime()/1000));

        Message message = new Message();
        message.setUid(video.getUid());
        message.setVid(video.getId());
        message.setOid(user.getId());
        message.setName(user.getNickname());
        message.setContent(Constant.MESSAGE_CONTENT5 + (!(content == null || content.equals(""))? content:""));
        message.setType(Constant.MESSAGE_TYPE2);
        message.setAvatar(user.getAvatar());
        message.setImage_path(video.getImage_path());
        message.setMessage_time((int)(new Date().getTime()/1000));
        commentService.commentVideo(comment, message);

        return ResponseBean.ok();
    }

    @RequestMapping(value="/queryVideoCommentList")
    public ResponseBean queryVideoCommentList(HttpServletRequest request){
        String vid = request.getParameter("vid");
        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少视频ID参数!");

        String page = request.getParameter("page");
        if(page == null || page.equals(""))
            return ResponseBean.fail("缺少页数参数!");

        String size = request.getParameter("size");
        if(size == null || size.equals(""))
            return ResponseBean.fail("缺少页数大小参数!");

        return ResponseBean.ok(commentService.queryCommentList(Long.parseLong(vid), Integer.parseInt(page), Integer.parseInt(size)));
    }

}
